package sealte.hvpms.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import sealte.hvpms.dao.AdminDAO;
import sealte.hvpms.dao.MessageDAO;
import sealte.hvpms.pojo.Message;
import sealte.hvpms.util.DBConnection;

public class MessageDAOImpl implements MessageDAO {
	private Connection conn = null;
	private PreparedStatement ps = null;
	private ResultSet rs = null;
	
	public boolean addNew(Message mes,int from){
		boolean flag = false;
		String sql = "insert into hvpms_messages(message_title,message_content,message_from) values(?,?,?)";
		try{
			conn = DBConnection.getConnection();
			ps = conn.prepareStatement(sql);
			ps.setString(1, mes.getTitle());
			ps.setString(2, mes.getContent());
			ps.setInt(3, from);
			ps.executeUpdate();
			flag = true;
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DBConnection.close(null, ps, conn);
		}
		return flag;
	}
	
	public int getAllRecorders(String kw){
		int ar = 0;
		String sql = null;
		if(kw==null||kw.equals("")){
			sql = "SELECT COUNT(message_id) from hvpms_messages";
		}else{
			sql = "SELECT COUNT(message_id) from hvpms_messages WHERE message_title LIKE ?";
		}
		try{
			conn = DBConnection.getConnection();
			ps = conn.prepareStatement(sql);
			if (!(kw == null || kw.equals(""))){
				ps.setString(1, "%" + kw + "%");
			}
			rs = ps.executeQuery();
			if(rs.next()){
				ar = rs.getInt(1);
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DBConnection.close(rs, ps, conn);
		}
		return ar;
	}
	
	public List<Message> listAll(int currentPage,int lineSize,String kw){
		List<Message> all = new ArrayList<Message>();
		AdminDAO ab = new AdminDAOImpl();
		int prevDates = (currentPage -1) * lineSize;
		String sql = null;
		if (kw == null || kw.equals("")) {
			sql = "SELECT * FROM hvpms_messages limit ?,?";
		} else {
			sql = "SELECT * FROM hvpms_messages WHERE message_title LIKE ? limit ?,?";
		}
		try{
			conn = DBConnection.getConnection();
			ps = conn.prepareStatement(sql);
			if (!(kw == null || kw.equals(""))) {
				ps.setString(1, "%" + kw + "%");
				ps.setInt(2, prevDates);
				ps.setInt(3, lineSize);
			}else{
				ps.setInt(1, prevDates);
				ps.setInt(2, lineSize);
			}
			rs = ps.executeQuery();
			while(rs.next()){
				Message mes = new Message();
				mes.setId(rs.getInt("message_id"));
				mes.setTitle(rs.getString("message_title"));
				mes.setFrom(ab.getNameByID(rs.getInt("message_from")));
				mes.setDate(rs.getTimestamp("message_date").toString().substring(0, 19));
				all.add(mes);
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DBConnection.close(rs, ps, conn);
		}
		return all;
	}
	
	public boolean deleteByID(int id){
		boolean flag = false;
		String sql = "delete from hvpms_messages where message_id=?";
		try{
			conn = DBConnection.getConnection();
			ps = conn.prepareStatement(sql);
			ps.setInt(1, id);
			ps.executeUpdate();
			flag = true;
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DBConnection.close(null, ps, conn);
		}
		return flag;
	}
	
	public boolean batchDeleteByID(int[] mes_ids){
		boolean flag = false;
		String sql = "delete from hvpms_messages where message_id=?";
		try{
			conn = DBConnection.getConnection();
			ps = conn.prepareStatement(sql);
			for(int i=0;i<mes_ids.length;i++){
				ps.setInt(1, mes_ids[i]);
				ps.executeUpdate();
			}
			flag = true;
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DBConnection.close(null, ps, conn);
		}
		return flag;
	}
	
	public Message getMessageByID(int id){
		Message mes = null;
		String sql = "select * from hvpms_messages where message_id=?";
		try{
			conn = DBConnection.getConnection();
			ps = conn.prepareStatement(sql);
			ps.setInt(1, id);
			rs = ps.executeQuery();
			if(rs.next()){
				mes = new Message();
				mes.setId(id);
				mes.setTitle(rs.getString("message_title"));
				mes.setContent(rs.getString("message_content"));
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DBConnection.close(rs, ps, conn);
		}
		return mes;
	}
	
	public boolean modifyMessage(Message mes){
		boolean flag = false;
		String sql = "update hvpms_messages set message_title=?,message_content=? where message_id=?";
		try{
			conn = DBConnection.getConnection();
			ps = conn.prepareStatement(sql);
			ps.setString(1, mes.getTitle());
			ps.setString(2, mes.getContent());
			ps.setInt(3, mes.getId());
			ps.executeUpdate();
			flag = true;
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DBConnection.close(null, ps, conn);
		}
		return flag;
	}
}
